package com.fox.stock.config;


import com.fox.stock.pojo.vo.TaskThreadPoolInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;


/**
 * 配置线程池的配置类
 */

@Configuration
public class TaskExecutePoolConfig {

    @Autowired
    private TaskThreadPoolInfo info;

    @Bean(name = "threadPoolTaskExecutor", destroyMethod = "shutdown")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor(){
        // 构建线程池对象
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 设置核心线程数
        executor.setCorePoolSize(info.getCorePoolSize());
        // 设置最大线程数
        executor.setMaxPoolSize(info.getMaxPoolSize());
        // 设置线程的空闲时间
        executor.setKeepAliveSeconds(info.getKeepAliveSeconds());
        // 设置等待队列长度
        executor.setQueueCapacity(info.getQueueCapacity());
//        // 设置拒绝策略
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());

        return executor;
    }

}
